package com.cx.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 *
 */
public class ReentrantLockTest {

    private static  int sum = 0;
//    private static SimpleLock lock = new SimpleLock();
    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(()->{
                //加锁
                lock.lock();
                try {
                    for (int j = 0; j < 10000; j++) {
                        sum++;
                        // Todo  异常
                    }
                } finally {
                    // 释放锁
                    lock.unlock();
                }
            });
            thread.start();
        }
        try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println(sum);

    }

}
